<?php

declare (strict_types=1);
namespace App\Model;

use App\Constants\Constant;
use Hyperf\DbConnection\Model\Model;
use Qbhy\HyperfAuth\Authenticatable;

/**
 * @property int $user_id 
 * @property string $thumbnail 
 * @property string $username 
 * @property string $add_time
 */
class User extends Model implements Authenticatable
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['user_id' => 'integer'];

    protected $primaryKey = 'user_id';

    public $timestamps = false;

    protected $hidden = ['pivot'];

    public function getId()
    {
        return $this->user_id;
    }

    public static function retrieveById($key): ?Authenticatable
    {
        return self::query()->find($key);
    }

    public function wechatInfo(){
        return $this->hasOne(UserWechatInfo::class,"user_id","user_id");
    }

    public function childs(){
        return $this->belongsToMany(Child::class,'child_user_mid','user_id','child_id');
    }

    public function score(){
        return $this->hasOne(UserScore::class,"user_id",'user_id');
    }

    public function payments(){
        return $this->hasMany(Payment::class,"user_id","user_id")->where([['origin',Constant::ORIGIN_ACTIVITY],["pay_status",Constant::PAY_STATUS_SUCCESS]]);
    }
}